Imports System.ComponentModel

Public Class frmLoadingForm

    Private ms_oThread As System.Threading.Thread
    Private m_ParentForm As Form

    Public ReadOnly Property IsBusy() As Boolean
        Get
            Return BackgroundWorker1.IsBusy
        End Get
    End Property

    Public Sub New(Optional ByVal ParentForm As Form = Nothing)
        InitializeComponent()

        'm_ParentForm = ParentForm

        ms_oThread = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf LoadingWindows))
        ms_oThread.IsBackground = False
        ms_oThread.SetApartmentState(Threading.ApartmentState.STA)
        ms_oThread.Start()
    End Sub

    'Public Sub New(ByVal method_name As String)
    '    InitializeComponent()

    '    'm_ParentForm = ParentForm
    '    Label1.Text = method_name

    '    ms_oThread = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf LoadingWindows))
    '    ms_oThread.IsBackground = False
    '    ms_oThread.SetApartmentState(Threading.ApartmentState.STA)
    '    ms_oThread.Start()
    'End Sub

    Public Sub ShowLoading()
        InitializeComponent()

        'm_ParentForm = ParentForm

        Me.Terminate()

        ms_oThread = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf LoadingWindows))
        ms_oThread.IsBackground = False
        ms_oThread.SetApartmentState(Threading.ApartmentState.STA)
        ms_oThread.Start()
    End Sub

    Private Sub LoadingWindows()
        'If m_ParentForm IsNot Nothing Then
        '    Me.ShowDialog(m_ParentForm)
        'Else
        Me.ShowDialog()
        'End If
    End Sub

    Public Sub Terminate()
        'Me.Hide()
        BackgroundWorker1.CancelAsync()
        ms_oThread.Abort()
    End Sub


    Private Sub LoadingForm_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Leave
        BackgroundWorker1.CancelAsync()
    End Sub ' Inherits Form from the designer.vb file

    'Private _worker As BackgroundWorker

    'Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    '    MyBase.OnLoad(e)

    '    _worker = New BackgroundWorker()
    '    AddHandler _worker.DoWork, AddressOf WorkerDoWork
    '    AddHandler _worker.RunWorkerCompleted, AddressOf WorkerCompleted

    '    _worker.RunWorkerAsync()
    'End Sub

    '' This is executed on a worker thread and will not make the dialog unresponsive.  If you want
    '' to interact with the dialog (like changing a progress bar or label), you need to use the
    '' worker's ReportProgress() method (see documentation for details)
    'Private Sub WorkerDoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
    '    Label1.Text = "Loading..."
    'End Sub


    '' This is executed on the UI thread after the work is complete.  It's a good place to either
    '' close the dialog or indicate that the initialization is complete.  It's safe to work with
    '' controls from this event.
    'Private Sub WorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
    '    Me.DialogResult = Windows.Forms.DialogResult.OK
    '    Me.Close()
    'End Sub




    Private Sub LoadingForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If BackgroundWorker1.IsBusy <> True Then
            BackgroundWorker1.RunWorkerAsync()
        End If
    End Sub


    Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim count As Integer = 0

        While 1 = 1
            If (worker.CancellationPending = True) Then
                e.Cancel = True
                Exit While
            Else
                worker.ReportProgress(count)
                count += 10
                If count > 100 Then
                    count = 0
                End If
                System.Threading.Thread.Sleep(100)
            End If
        End While

    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        pgbLoading.Value = e.ProgressPercentage
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
        ms_oThread.Abort()
    End Sub


End Class